﻿

var submit = function () {
    if ($('form .validate').length) return false;
    $.prompt("Do you want to save this authorized item?", {
        title: "Confirm",
        buttons: { "Yes": true, "Cancel": false },
        submit: function (e, v) {
            // use e.preventDefault() to prevent closing when needed or return false. 
            // e.preventDefault(); 
            if (!v) return;
            if ($('#from-date').val() == '') {
                $('#from-date').parent().append('<label class="validate" style="color: red" title="Start date is not null.">*</label>');
                return;
            }
            if ($('#to-date').val() == '') {
                $('#to-date').parent().append('<label class="validate" style="color: red" title="End date is not null.">*</label>');
                return;
            }

            $.ajax({
                type: "POST",
                url: "/Pages/AuthorizerForm.aspx/SaveAuthorized",
                data: JSON.stringify({
                    authorized: {
                        AuthorizedPerson: $('.authorizedPerson').val(),
                        Remark: $('#remark').val(),
                        StartDate: $('#from-date').val() + ' ' + $('#fromTime').val(),
                        EndDate: $('#to-date').val() + ' ' + $('#toTime').val()
                    }
                }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data.d) {
                        $.prompt('You have saved successfully.');
                        window.location.href = '/Pages/AuthorizationList.aspx';
                    }
                },
                error: function () {
                }
            });
        }
    });

    return false;
};

var reset = function () {

    $('#from-date').val('');
    $('#to-date').val('');
    $('#fromTime').val('');
    $('#toTime').val('');
    $('#remark').val('');
    $('.authorizedPerson').val(null);

    return false;
};

// when start time change, update minimum for end timepicker

function tpStartSelect(time, endTimePickerInst) {
    var compare = compareDiff($('.from.datepicker').datepicker('getDate'), $('.to.datepicker').datepicker('getDate'));
    var hour = (compare === 0) ? endTimePickerInst.hours : 0;
    var minute = (compare === 0) ? endTimePickerInst.minutes : 0;
    $('.timepicker.end').timepicker('option', {
        minTime: {
            hour: hour,
            minute: minute
        }
    });
}

// when end time change, update maximum for start timepicker

function tpEndSelect(time, startTimePickerInst) {
    var compare = compareDiff($('.from.datepicker').datepicker('getDate'), $('.to.datepicker').datepicker('getDate'));
    var hour = (compare === 0) ? startTimePickerInst.hours : 23;
    var minute = (compare === 0) ? startTimePickerInst.minutes : 59;
    $('.timepicker.start').timepicker('option', {
        maxTime: {
            hour: hour,
            minute: minute
        }
    });
}

function compareDiff(startDate, endDate) {
    var difference = (endDate - startDate) / (86400000);
    return difference;
}

$(document).ready(function () {
    var today = new Date();

    $(".from.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        minDate: today,
        onClose: function (selectedDate) {
            $(".to.datepicker").datepicker("option", "minDate", selectedDate);

            var compare = compareDiff(selectedDate, $(".to.datepicker").datepicker('getDate'));
            var hour = (compare === 0) ? $('.timepicker.end').timepicker('getHour') : 23;
            var minute = (compare === 0) ? $('.timepicker.end').timepicker('getMinute') : 59;
            $('.timepicker.start').timepicker('option', {
                maxTime: {
                    hour: hour,
                    minute: minute
                }
            });
            compare = compareDiff($('.timepicker.start').timepicker('getTimeAsDate'),
                $('.timepicker.end').timepicker('getTimeAsDate'));
            if (compare < 0) {
                $('.timepicker.start').timepicker('setTime', $('.timepicker.end').timepicker('getTime'));
            }
        }
    });
    $(".to.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        minDate: today,
        onClose: function (selectedDate) {
            $(".from.datepicker").datepicker("option", "maxDate", selectedDate);

            var compare = compareDiff($(".from.datepicker").datepicker('getDate'), selectedDate);
            var hour = (compare === 0) ? $('.timepicker.start').timepicker('getHour') : 0;
            var minute = (compare === 0) ? $('.timepicker.start').timepicker('getMinute') : 0;
            $('.timepicker.end').timepicker('option', {
                minTime: {
                    hour: hour,
                    minute: minute
                }
            });
            compare = compareDiff($('.timepicker.start').timepicker('getTimeAsDate'),
                $('.timepicker.end').timepicker('getTimeAsDate'));
            if (compare < 0) {
                $('.timepicker.end').timepicker('setTime', $('.timepicker.start').timepicker('getTime'));
            }
        }
    });

    $('.timepicker.start').timepicker({
        onSelect: tpStartSelect,
        showOn: 'button',
        button: '.timepicker_button_trigger.start'
    });
    $('.timepicker.end').timepicker({
        onSelect: tpEndSelect,
        showOn: 'button',
        button: '.timepicker_button_trigger.end'
    });

    $('.datepicker').attr('readonly', true);
    $('.timepicker').attr('readonly', true);

    $('#submit').click(submit);
    $('#reset').click(reset);
});